#!/usr/bin/perl
#lvcreate.cgi
require './lvm-lib.pl';
&ReadParse();
my ($err_msg, $found, $vgname, @vginfo, @lvinfo, $v, $vg);
$vgname = $in{'vg'};
if(!$vgname){
	$err_msg = $text{'noselectvg'};
	goto ERROR_OUT;
}
@vginfo = get_vg_info();
@lvinfo = get_lv_info();
if(@lvinfo > 15){
	$err_msg = $text{'err_reachmaxlvnum'};
	goto ERROR_OUT;
} 
$found = 0;
foreach $v (@vginfo) {
	if($v->{'vg'} eq $vgname){
		$vg = $v;
		$found = 1;
	}
}
if(!$found){
	$err_msg = $text{'err_vgnofound'};
	goto ERROR_OUT;
}
if($vg->{'free'} eq 0){
	$err_msg = $text{'err_vg_nofreesize'};
	goto ERROR_OUT;
}
&header($text{'title_lvcreate'}, "", "createlv", undef, 0);
print "<form name=form_lvcreate action=index.cgi method=post>\n";
print "<table class=mode1 width=80%>";
print "<tr><td width=150><b>$text{'index_vg'}</b></td><td>$vg->{'vg'}</td></tr>\n";
print "<tr><td width=150><b>$text{'index_vg_free'}</b></td><td>$vg->{'free'}</td></tr>\n";
print "<input type=hidden name=vgfree value=$vg->{'free'}>";
print "<tr><td width=150><b>$text{'lvname'}</b></td>";

my @freevolumes = get_free_volumes(@lvinfo);
my $fv;
my $i = 0;
print "<td><select name=volname class=drop>";
foreach $fv (@freevolumes) {
	if ($i == 0) { 
		print "<option selected value=$fv>$fv";
	}
	else {
		print "<option value=$fv>$fv";
	}
	$i++;
}
print "</select></td></tr>\n";
my $g = "GB";
my $m = "MB";
print "<tr><td width=150><b>$text{'lvsize'}</b></td>";
print "<td><input name=lvsize class=text size=10> ";
print "<select name=unit><option selected value=$g>$g<option value=$m>$m</select>";
print "</td></tr>\n";
print "</table>\n";

print "<table border=0 class=mode1>\n";
print "<tr><td>";
print "<input type=submit name=create class=button1 value=$text{'bt_create'} onclick='return SetEditValue(this)'>\n"; 
print "</td>";
print "<td><input type=submit name=cancel class=button1 value=$text{'bt_cancel'}></td>";
print "</tr>\n";
print "</table>\n";
print "<input type=hidden name=vgname value=$vg->{'vg'}>";
print <<EOF;
<script>
function SetEditValue(obj)
{
        if (obj == document.form_lvcreate.create) {
                document.form_lvcreate.action = 'act_lvcreate.cgi';
        }
}
</script>
EOF
print "</form>\n";
&footer("/lvm", $text{'index'});
return;
ERROR_OUT:
&error_setup($text{'crt_err_title'});
&error($err_msg);
